package service.data.controller;

import org.springframework.beans.factory.annotation.Value;
import service.data.utils.JSONResult;
import glider.dataservicestarter.dataservice.impl.DataServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
public class ApiController {
    @Value("${server.port}")
    private String serverPort;

    @Resource
    private DataServiceImpl _DataService = new DataServiceImpl("./druid_dev.properties");

    @GetMapping(value = "/gets/{tablename}")
    public JSONResult gets(@PathVariable String tablename){
        List<Map<String,Object>> objlist = null;
        try {
            objlist = this._DataService.gets(tablename);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return JSONResult.ok(1,objlist);
    }

    @GetMapping(value = "/get/{tablename}/{id}")
    public JSONResult get(@PathVariable String tablename,@PathVariable int id){
        Map<String,Object> obj = null;
        try {
            obj = this._DataService.get(tablename,id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return JSONResult.ok(1,obj);
    }

    @PostMapping(value = "/add/{tablename}")
    public JSONResult add(@PathVariable String tablename,@RequestBody Map<String,Object> params){
        int rs = 0;
        try {
            rs = this._DataService.add(tablename,params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return JSONResult.ok(1,rs);
    }

    @PutMapping(value = "/update/{tablename}/{id}")
    public JSONResult update(@PathVariable String tablename,@PathVariable int id,@RequestBody Map<String,Object> item){
        int rs = 0;
        try {
            rs = this._DataService.update(tablename,item,id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return JSONResult.ok(1,rs);
    }

    @DeleteMapping(value = "/delete/{tablename}/{id}")
    public JSONResult delete(@PathVariable String tablename,@PathVariable int id){
        int rs = 0;
        try {
            rs = this._DataService.delete(tablename,id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return JSONResult.ok(1,rs);
    }

    @GetMapping(value = "/getslimit/{tablename}/{begin}/{count}")
    public JSONResult getlimit(@PathVariable String tablename,@PathVariable int begin,@PathVariable int count){
        Map<String,Object> objlist = null;
        try {
            objlist = this._DataService.getsLimit(tablename,begin,count);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return JSONResult.ok(1,objlist);
    }

    @GetMapping(value = "/test_gets")
    public JSONResult testba(){
        List<Map<String,Object>> objlist = null;
        try {
            objlist = this._DataService.gets("users");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return JSONResult.ok(1,objlist);
    }

    @GetMapping(value = "/query/{tablename}/{selectreg}/{wherereg}")
    public JSONResult query(@PathVariable String tablename,@PathVariable String selectreg,@PathVariable String wherereg){
        List<Map<String,Object>> objlist = null;
        try {
            objlist = this._DataService.queryListByReg(tablename,selectreg,wherereg);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return JSONResult.ok(1,objlist);
    }

    @GetMapping(value = "/querylimit/{tablename}/{selectreg}/{wherereg}/{begin}/{count}")
    public JSONResult querylimit(@PathVariable String tablename,@PathVariable String selectreg,@PathVariable String wherereg,@PathVariable int begin,@PathVariable int count){
        Map<String,Object> objlist = null;
        try {
            objlist = this._DataService.queryListByRegAndLimit(tablename,selectreg,wherereg,begin,count);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return JSONResult.ok(1,objlist);
    }

    @ApiOperation(value="获取表结构", notes="获取表结构")
    @GetMapping(value = "/get_structure/{tablename}")
    public JSONResult getStructure(@PathVariable String tablename){
        System.out.println("tablename:"+tablename);
        List<Map<String,Object>> objlist = new ArrayList<Map<String,Object>>();
        objlist = this._DataService.getStructure(tablename);
        System.out.println(objlist.toString());
        return JSONResult.ok(1,objlist);
    }

    @ApiOperation(value="获取树结构", notes="获取树结构")
    @GetMapping(value = "/get_tree/{tablename}/{id}/{level}")
    public JSONResult getTree(@PathVariable String tablename,@PathVariable String id,@PathVariable String level){
        System.out.println("tablename:"+tablename);
        List<Map<String,Object>> objlist = new ArrayList<Map<String,Object>>();
        objlist = this._DataService.getTree(tablename,id,level);
        System.out.println(objlist.toString());
        return JSONResult.ok(1,objlist);
    }

}
